capture log close
clear all
set maxvar 10000
*set matsize 11000
set more off
pause off
# delimit;

local crime "";
local output "";


/* 
Table B1
Compare CJARS sample to non-CJARS sample
*/


/* ****************************************** */
/* **** SAMPLE SELECTION FOR MAIN SAMPLE **** */
/* ****************************************** */

use 	 "`crime'\prwora_age18_crimekid.dta", clear;
duplicates drop hun, force ;

gen dobdd_stata	=mdy(month(dob),day(dob),year(dob)+18); 
gen dobdd_run	=dobdd_stata-mdy(08,22,1996);

gen dobwk_stata	=wofd(mdy(month(dob),day(dob),year(dob)+18));
gen dobwk_run	=dobwk_stata-wofd(mdy(08,22,1996));

gen dobmo_stata	=ym(year(dob)+18,month(dob)) ;
gen dobmo_run	=dobmo_stata-mofd(mdy(08,22,1996)) ;

keep if year(first_pay_dte)<1996 ;
count;


/* ******************************* */
/* **** MERGE IN CJARS SAMPLE **** */
/* ******************************* */

merge 1:1 hun using "`crime'\prep2\crime_prwora_prepforreg.dta", keepusing(keep*) ;

replace keep = 0 if keep == . ;
tab keep, m ;

keep if abs(dobdd_run) <= 264 ;


/* ******************************************* */
/* **** CREATE CHARACTERISTICS TO COMPARE **** */
/* ******************************************* */

/* Diagnosis */
tab n_diag1cat, m;
gen byte n_diag1_infec=(n_diag1cat==1);
gen byte n_diag1_neo=(n_diag1cat==2);
gen byte n_diag1_endo=(n_diag1cat==3);
gen byte n_diag1_blood=(n_diag1cat==4);
gen byte n_diag1_mental=(n_diag1cat==5);
gen byte n_diag1_nerv=(n_diag1cat==6);
gen byte n_diag1_sense=(n_diag1cat==7);
gen byte n_diag1_circ=(n_diag1cat==8);
gen byte n_diag1_resp=(n_diag1cat==9);
gen byte n_diag1_dig=(n_diag1cat==10);
gen byte n_diag1_gu=(n_diag1cat==11);
gen byte n_diag1_preg=(n_diag1cat==12);
gen byte n_diag1_skin=(n_diag1cat==13);
gen byte n_diag1_musc=(n_diag1cat==14);
gen byte n_diag1_cong=(n_diag1cat==15);
gen byte n_diag1_nat=(n_diag1cat==16);
gen byte n_diag1_ill=(n_diag1cat==17);
gen byte n_diag1_inj=(n_diag1cat==18);
gen byte n_diag1_none=(n_diag1cat==.);

/* Family structure */
tab toa, m;
gen byte n_fam_singmom=	(toa=="DM" | toa=="BM");
gen byte n_fam_nopar=	(toa=="DC" | toa=="BC");
tab n_fam_singmom, m;
tab n_fam_nopar, m;

/* Male */
tab n_male, m;

/* Severity */
tab dibmdr, m;
gen byte n_dibmdr_miss=(dibmdr=="");
gen byte n_dibmdr_mip=(dibmdr=="3");
gen byte n_dibmdr_mine=(dibmdr=="5" | dibmdr=="7");
gen byte n_dibmdr_mie=(!n_dibmdr_miss & !n_dibmdr_mip & !n_dibmdr_mine);
tab n_dibmdr_mie, m;
tab n_dibmdr_mip, m;
tab n_dibmdr_mine, m;

/* Age at entry */
tab n_firstage, m;

/* Parent earnings and household income */
local earncut = 5000 ;

order n_hhearns1996_*  n_combpay_* n_hhmbayr1996_* n_hhcombpay1996_* n_sibpayall_* ;
forval yr=1990(1)2012 { ;
	gen n_inchh1996_`yr' = n_hhearns1996_`yr' + n_combpay_`yr' + n_hhmbayr1996_`yr' + n_hhcombpay1996_`yr' + n_sibpayall_`yr' if n_hhearns1996_`yr' != . ;
} ;
forval yr=1990(1)2012 { ;
	gen n_inchh_`yr' = n_hhearns_`yr' + n_combpay_`yr' + n_hhmbayr_`yr' + n_hhcombpay_`yr' + n_sibpayall_`yr' if n_hhearns_`yr' != . ;
} ;
summ n_inchh* ;

egen n_earnhh9702=rowmean(n_hhearns_1997-n_hhearns_2002) ;
egen n_earnhh9712=rowmean(n_hhearns_1997-n_hhearns_2012) ;
egen n_earnhhpre9095=rowmean(n_hhearns_1990-n_hhearns_1995) ;

forval yr=1990(1)2012 { ;
	gen byte temp`yr' = (n_hhearns_`yr' > `earncut') ;
	replace temp`yr' = . if n_hhearns_`yr' == . ;
} ;
egen n_emphhpost9702=rowmean(temp1997-temp2002) ;
egen n_emphhpost9712=rowmean(temp1997-temp2012) ;
egen n_emphhpre9095=rowmean(temp1990-temp1995) ;
drop temp* ;


/* ********************************************** */
/* **** SUMMARY STATISTICS BY CJARS COVERAGE **** */
/* ********************************************** */
# delimit;

local tot = 0 ;
foreach var of varlist 
	n_diag1_* n_fam_* n_dibmdr_* n_firstage* n_earnhhpre9095 n_emphhpre9095
	{ ;
	local tot = `tot' + 1 ;
	local pvals_lab "`pvals_lab' `var' " ;
	} ;

matrix pvals=J(1,`tot',.) ;
local count = 0 ;

foreach var of varlist 
	n_diag1_* n_fam_* n_dibmdr_* n_firstage* n_earnhhpre9095 n_emphhpre9095
	{ ;
		local count = `count' + 1 ;
		ttest `var', by(keep) ;
		matrix pvals[1,`count'] = r(p) ;
	} ;
	
preserve ;
clear ;
svmat pvals ;
rename (pvals*) (`pvals_lab') ;
tempfile pvals ;
save `pvals' ;
restore ;

gen count = 1 ;
collapse 
	(mean) n_diag1_* n_fam_* n_dibmdr_* n_firstage* n_earnhhpre9095 n_emphhpre9095
	(count) count 
	, by(keep) ;
	
append using `pvals' ;
save "`output'\prwora_crime_comparesamples.dta", replace ;

capture log close;
